// 数字范围按位与

package Leetcode;

class Solution201_1 {
    public int rangeBitwiseAnd(int left, int right) {
        if (left == 0 || (left <= Integer.MAX_VALUE / 2 && right >= Integer.MAX_VALUE / 2 + 1))
            return 0;
        int result = left;
        for (int i = left + 1; i <= right; i++)
            result = result & i;
        return result;
    }
}

class Solution201_2 {
    public int rangeBitwiseAnd(int left, int right) {
        int count = 0;
        while (left != right) {
            left = left >>> 1;
            right = right >>> 1;
            count++;
        }
        return left << count;
    }
}

class Solution201_3 {
    public int rangeBitwiseAnd(int left, int right) {
        while (left < right)
            right = right & (right - 1);
        return right;
    }
}